package com.senven.books.modules.booksManager.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.senven.books.modules.booksManager.entity.Book;
import com.senven.books.modules.booksManager.entity.param.BookParam;
import com.senven.books.modules.booksManager.entity.vo.BookVo;
import com.senven.books.modules.booksManager.mapper.BookManagerMapper;
import com.senven.books.modules.booksManager.service.BookManagerService;
import com.senven.books.modules.system.log.util.LogUtil;
import com.senven.books.modules.system.log.util.SessionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description TODO()
 * @author 龙声益
 * @date 2024/7/239:03
 * @version 1.0
 * @since 1.0
 * @修改记录
 */
@Slf4j
@Service
public class BookManagerServiceImpl extends ServiceImpl<BookManagerMapper, Book> implements BookManagerService {

    @Resource
    private BookManagerMapper bookManagerMapper;

    @Autowired
    private LogUtil logUtil;

    @Override
    @Transactional
    public int createBook(Book book) {
        try {
            Long userId = SessionUtil.getUserInfo().getSysUser().getId();
            book.setCreateUser(userId);
            book.setDeleted(0);
            Date notNullDate = new Date();
            book.setCreateTime(notNullDate);
            return this.bookManagerMapper.insert(book);
        } catch (Exception e) {
            log.error("创建图书信息失败，" + e.getMessage(), e);
        }
        return 0;
    }

    @Override
    @Transactional
    public int updateBookBook(Book book) {
        try {
            Long userId = SessionUtil.getUserInfo().getSysUser().getId();
            book.setUpdateUser(userId);
            book.setDeleted(0);
            Date notNullDate = new Date();
            book.setUpdateTime(notNullDate);
            return this.bookManagerMapper.updateById(book);
        } catch (Exception e) {
            log.error("修改图书信息失败，" + e.getMessage(), e);
        }
        return 0;
    }

    @Override
    @Transactional
    public int deleteById(Long id) {
        try {
            return this.bookManagerMapper.deleteById(id);
        } catch (Exception e) {
            log.error("删除图书信息失败，" + e.getMessage(), e);
        }
        return 0;
    }

    @Override
    public BookVo getBookVoById(Long id) {
        Book book = this.bookManagerMapper.selectById(id);
        if (book == null) {
            return null;
        }
        return new BookVo(book);
    }

    @Override
    public List<BookVo> getBookList(String id) {
        return null;
    }

    @Override
    public IPage<BookVo> getBooksByPage(Page<BookVo> page, BookParam param) {
        IPage<BookVo> listPage = bookManagerMapper.getBookListPage(page, param);
        return listPage;
    }
}
